package com.tan.lbs.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.tan.lbs.dao.GameDAO;
import com.tan.lbs.entity.Designer;
import com.tan.lbs.entity.Game;
import com.tan.lbs.entity.GameHeader;
import com.tan.lbs.util.HibernateUtil;
/**
 * 
 * Copyright by Zhensheng Tan only for Final Year Project Use.
 * Class description:
 * @author Zhensheng Tan 
 *
 */
public class GameDAOImpl implements GameDAO{

	@Override
	public String create(Game game) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(game);
		s.getTransaction().commit();
		return game.getGameID();
	}

	@Override
	public ArrayList<Game> searchByDesigner(String designerID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<Game> gs = s.createCriteria(Game.class).add(Restrictions.
				eq("designerID", designerID)).list();
		s.getTransaction().commit();
		return new ArrayList<Game>(gs);
	}

	@Override
	public Game readByID(String gameID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Game g = (Game)s.get(Game.class, gameID);
		s.getTransaction().commit();
		String status = g.getDesignStatus();
		if(status.equalsIgnoreCase("e")) g.setDesignStatus("In Edit");
		else if(status.equalsIgnoreCase("p")) g.setDesignStatus("Publish");
		return g;
	}

	@Override
	public void update(Game game) {
		if(game.getDesignStatus().equalsIgnoreCase("In Edit")) game.setDesignStatus("e");
		else if(game.getDesignStatus().equalsIgnoreCase("Publish")) game.setDesignStatus("p");
		
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.update(game);
		s.getTransaction().commit();
	}
	

}
